 /*
  * Copyright (c) 2021 Huawei Device Co., Ltd.
  * <p>
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * <p>
  * http://www.apache.org/licenses/LICENSE-2.0
  * <p>
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

package org.angmarc.app;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.aafwk.ability.delegation.IAbilityDelegator;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.StateElement;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import org.angmarch.views.NiceSpinner;
import org.angmarch.views.PopUpTextAlignment;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class NiceSpinnerUiTest {

    private Ability ability;
    private IAbilityDelegator abilityDelegator;

    @Before
    public void setUp() throws Exception {
        abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
        ability = EventHelper.startAbility(MainAbility.class);
        EventHelper.waitForActive(ability, 5);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @After
    public void tearDown() throws Exception {
        abilityDelegator.stopAbility(ability);
        abilityDelegator = null;
        EventHelper.clearAbilities();
    }

    @Test
    public void defaultDisplayTest() {
        NiceSpinner niceSpinner = (NiceSpinner) ability.findComponentById(ResourceTable.Id_nice_spinner);
        Image imageComponent = niceSpinner.getImageComponent();
        Assert.assertEquals(imageComponent.getVisibility(), Component.VISIBLE);
        Text textComponent = niceSpinner.getTextComponent();
        Assert.assertEquals("One", textComponent.getText());
    }

    @Test
    public void hideArrowTest() {
        NiceSpinner niceSpinner = (NiceSpinner) ability.findComponentById(ResourceTable.Id_nice_spinner);
        AbilityDelegatorRegistry.getAbilityDelegator().runOnUIThreadSync(new Runnable() {
            @Override
            public void run() {
                niceSpinner.hideArrow();
            }
        });
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Image imageComponent = niceSpinner.getImageComponent();
        Assert.assertEquals(imageComponent.getVisibility(), Component.INVISIBLE);
    }

    @Test
    public void displayDialogTest() {
        NiceSpinner niceSpinner = (NiceSpinner) ability.findComponentById(ResourceTable.Id_nice_spinner);
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, 0);
        EventHelper.inputSwipe(ability, niceSpinner, path, 1000);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Assert.assertNotNull(niceSpinner.getContentView());
    }

    @Test
    public void displayCustomColorTest() {
        NiceSpinner niceSpinner = (NiceSpinner) ability.findComponentById(ResourceTable.Id_tinted_nice_spinner);
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, 0);
        EventHelper.inputSwipe(ability, niceSpinner, path, 1000);
        ListContainer listContainer = niceSpinner.getContentView();
        Text component = (Text) listContainer.getComponentAt(1);
        int intColor = Color.getIntColor("#008577");
        int value = component.getTextColor().getValue();
        Assert.assertEquals(intColor, value);
    }

    @Test
    public void customDisplayTest() {
        NiceSpinner niceSpinner = (NiceSpinner) ability.findComponentById(ResourceTable.Id_tinted_nice_spinner);
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, 0);
        EventHelper.inputSwipe(ability, niceSpinner, path, 1000);
        ListContainer listContainer = niceSpinner.getContentView();
        Text component = (Text) listContainer.getComponentAt(1);

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        AbilityDelegatorRegistry.getAbilityDelegator().runOnUIThreadSync(new Runnable() {
            @Override
            public void run() {
                Element backgroundElement = component.getBackgroundElement();
                StateElement stateElement = (StateElement) backgroundElement;
                Element element = stateElement.getStateElement(0);
                listContainer.executeItemClick(component, 1, 1);
                Element currentElement = element.getCurrentElement();
                Assert.assertEquals("stateElement", element, currentElement);
            }
        });
    }

    @Test
    public void setTextAlimentTest() {
        NiceSpinner niceSpinner = (NiceSpinner) ability.findComponentById(ResourceTable.Id_niceSpinnerJson);
        niceSpinner.setPopupTextAliment(PopUpTextAlignment.START);
        niceSpinner.setStrArrayData(ResourceTable.Strarray_courses);
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, 0);
        EventHelper.inputSwipe(ability, niceSpinner, path, 1000);
        ListContainer listContainer = niceSpinner.getContentView();
        Text component = (Text) listContainer.getComponentAt(1);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Assert.assertEquals(component.getTextAlignment(), TextAlignment.START);
    }

    @Test
    public void setStringArrayTest() {
        NiceSpinner niceSpinner = (NiceSpinner) ability.findComponentById(ResourceTable.Id_niceSpinnerJson);
        niceSpinner.setStrArrayData(ResourceTable.Strarray_courses);
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, 0);
        EventHelper.inputSwipe(ability, niceSpinner, path, 1000);
        ListContainer listContainer = niceSpinner.getContentView();
        Text component = (Text) listContainer.getComponentAt(1);

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        AbilityDelegatorRegistry.getAbilityDelegator().runOnUIThreadSync(new Runnable() {
            @Override
            public void run() {
                listContainer.executeItemClick(component, 1, 1);
            }
        });
        Assert.assertEquals("Math", niceSpinner.getTextComponent().getText());
    }
}